package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ExamPaperMapper;
import com.youlai.boot.system.service.ExamPaperService;
import com.youlai.boot.system.model.entity.ExamPaper;
import com.youlai.boot.system.model.form.ExamPaperForm;
import com.youlai.boot.system.model.query.ExamPaperQuery;
import com.youlai.boot.system.model.vo.ExamPaperVO;
import com.youlai.boot.system.converter.ExamPaperConverter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 试卷管理服务实现类
 *
 * @author youlaitech
 * @since 2025-07-04 08:25
 */
@Service
@RequiredArgsConstructor
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {

    private final ExamPaperConverter examPaperConverter;

    /**
    * 获取试卷管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ExamPaperVO>} 试卷管理分页列表
    */
    @Override
    public IPage<ExamPaperVO> getExamPaperPage(ExamPaperQuery queryParams) {
        Page<ExamPaperVO> pageVO = this.baseMapper.getExamPaperPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    
	@Override
	public IPage<ExamPaperVO> getExamUserPaperPage(ExamPaperQuery queryParams) {
		  Page<ExamPaperVO> pageVO = this.baseMapper.getExamUserPaperPage(
	                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
	                queryParams
	        );
	        return pageVO;
	}
    
    /**
     * 获取试卷管理表单数据
     *
     * @param id 试卷管理ID
     * @return 试卷管理表单数据
     */
    @Override
    public ExamPaperForm getExamPaperFormData(Long id) {
        ExamPaper entity = this.getById(id);
        return examPaperConverter.toForm(entity);
    }
    
    /**
     * 新增试卷管理
     *
     * @param formData 试卷管理表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveExamPaper(ExamPaperForm formData) {
        ExamPaper entity = examPaperConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新试卷管理
     *
     * @param id   试卷管理ID
     * @param formData 试卷管理表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateExamPaper(Long id,ExamPaperForm formData) {
        ExamPaper entity = examPaperConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除试卷管理
     *
     * @param ids 试卷管理ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteExamPapers(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的试卷管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

	@Override
	public List<Map<String, Object>> getPagerList() {
		List<Map<String, Object>> list = this.baseMapper.getPagerList();
	        return list;
	}



}
